﻿
<div id="main-wrapper">
	<h1>开启讲座</h1>

	<form id="form-open">
		<div class="form-group">
			<div class="input-group">
				<input type="text" class="form-control" name="lname" placeholder="讲座名称">
				<span class="input-group-btn">
					<button class="btn btn-primary" href="#/enter">管理讲座</button>
				</span>
			</div>
		</div>
        <div class="form-group">
            <input type="password" class="form-control" name="lpwd1" placeholder="管理密码">
        </div>
        <div class="form-group">
            <input type="password" class="form-control" name="lpwd2" placeholder="重复密码">
        </div>
        <div class="form-group">
            <input type="text" class="form-control" name="nick" placeholder="讲者昵称">
        </div>
		<div class="form-group">
			<div class="input-group">
				<input type="text" class="form-control" name="vcode" placeholder="验证码">
                <span class="input-group-addon">
					<canvas id="canvas-vcode" width="70" height="18"></canvas>
				</span>
			</div>
		</div>
        <div class="form-group">
            <button type="submit" class="btn btn-primary btn-lg btn-block">
                开启讲座
            </button>
        </div>
	</form>

</div>


<script>
    var $formOpen = $('#form-open'),
        $canvasVcode = $('#canvas-vcode');
    
	$formOpen.ajaxForm({
		type: 'post',
		url: '/do/s/open',
		success: function(resObj){
            var ok = resObj['ok'], msg = resObj['msg'];
            msg && notify(msg, ok);
			if (ok) {
				loadFrame('#/view');
				updateNavbar();
			} else {
				showValicode();
                $formOpen.find('[name="vcode"]').val('');
			}
		}
	});

    showValicode();
    
	function showValicode() {
		$.get('/do/vcode?action=open', function(resObj){
			$.get('/do/vcode?action=open', function(resObj){
                if (! resObj['ok']) {
                    return notify('获取验证码失败', 0);
                }
				var vcodeInfo = resObj['vcodeInfo'],
					num1 = vcodeInfo['num1'],
					num2 = vcodeInfo['num2'],
					oper = vcodeInfo['oper'];
				$canvasVcode.drawVerificationCode(num1, num2, oper);
			});
		});
	}
</script>
